package com.jbm.jbmsupplier.activity.register.presenter

import android.content.Context
import android.os.CountDownTimer
import android.view.View
import com.jbm.core.common.MainServer
import com.jbm.enterapplication.InterfaceMa
import com.jbm.jbmsupplier.activity.loadhomepage.factoryhome.LoadHomeFactory
import com.jbm.jbmsupplier.activity.register.mode.Regiter_Mode_User
import com.jbm.jbmsupplier.activity.register.view.Register_Manage_Massages
import com.jbm.jbmsupplier.shared.SharedPreferActivity
import com.jbm.jbmsupplier.view.DialogLoading
import com.jbm.jbmsupplier.view.DoubleButtonDialog
import com.jbm.onehome.common.OHInterface

/**
 * Created by 家帮忙 on 2017/8/22.
 */
class Register_Present : Register_Present_Interface {
    private var sharedPreferActivity: SharedPreferActivity? = null
    var regitermodeuser = Regiter_Mode_User();//mode层的接口处理逻辑
    var register_manage_massagtes: Register_Manage_Massages? = null//view层的接口获取uri
    var context:Context?=null
     var  loadhomefactory: LoadHomeFactory?=null;

    constructor(register_manage_massagtes: Register_Manage_Massages?, context:Context?) {
        this.register_manage_massagtes = register_manage_massagtes
   this.context=context;
        sharedPreferActivity = SharedPreferActivity(context, true)
         loadhomefactory = LoadHomeFactory()
    }
    override fun getRegisterUserMassage(content: Context) {//注册为管理员
        if (register_manage_massagtes!!.getPhone().equals("")){
            register_manage_massagtes!!.getPhoneErr().error = "手机号不能为空"
            return
        }else if(register_manage_massagtes!!.getPhone().length<11){
            register_manage_massagtes!!.getPhoneErr().error = "手机号长度不符"
            return
        }else if(register_manage_massagtes!!.getValidateCode().equals("")){
            register_manage_massagtes!!.getValidateCodeErr().error = "验证码不能为空"
            return
        }else if(register_manage_massagtes!!.getPasWord().equals("")){
            register_manage_massagtes!!.getPasWordErr().error = "密码不能为空"
            return
        }else  if(register_manage_massagtes!!.getPasWordzc().equals("")){
            register_manage_massagtes!!.getPasWordErrzc().error = "密码不能为空"
            return
        }else if(!register_manage_massagtes!!.getPasWordzc().equals(register_manage_massagtes!!.getPasWordzc())){
            register_manage_massagtes!!.getPasWordErrzc().error = "两次密码不一致"
            return
        }else if(register_manage_massagtes!!.getUnitName().equals("")){
            register_manage_massagtes!!.getUnitNameErr().error = "单位名称不能为空"
            return
        } else if (loginid==null){
            register_manage_massagtes!!.getPasWordErr().error ="验证码已过期"
            return
        } else{
            dialogLoading = DialogLoading(context!!, "加载中.....", true)
            dialogLoading?.show()
            regitermodeuser.GainRegisterIntergace(register_manage_massagtes!!.getPhone(), register_manage_massagtes!!.getValidateCode(), register_manage_massagtes!!.getPasWord(), register_manage_massagtes!!.getPasWordzc(),loginid!!, register_manage_massagtes!!.getUnitName(),object :InterfaceMa<String>{
                override fun onSuccess(t: String, boolean: Boolean) {
//                    if (boolean==true){
                        setDialogding()
                        sharedPreferActivity?.setName(register_manage_massagtes!!.getPhone(), register_manage_massagtes!!.getPasWord())
                        sharedPreferActivity?.setBoolearLogin(true)//判断是否登陆
                        sharedPreferActivity?.setBoolearNetConn(true)//判断是否第一次连接服务器，如果登录成功就设为true 代表第一次链接成功
                        sharedPreferActivity?.setUserName(OHInterface.getG_server().nickname)
                        sharedPreferActivity?.setLoginid(OHInterface.getG_server().login_id)
                        MainServer.getG_server().m_local_id = OHInterface.getG_server().login_id
//                        ModeRxjavaNet.getInstantMode().objects(Observable.create<PrivilegeListEntity> { subscriber -> subscriber.onNext(ModeRxjavaNet.getInstantMode().getOhInter().getPersonalPrivilegeList(true, 0, OHInterface.getG_server().login_id)) }).subscribe(object : Observer<PrivilegeListEntity> {
//                            override fun onCompleted() {
//
//                            }
//
//                            override fun onError(e: Throwable) {
//
//                            }
//
//                            override fun onNext(privilegeListEntity: PrivilegeListEntity) {
//                                if (privilegeListEntity.m_code == 200) {
//                                    val arrPteson = ArrayList<PrivilegeListEntity>()
//                                    arrPteson.addAll(privilegeListEntity.priList)
//                                   PtesonalPrivile().setArrPteson(arrPteson, context)//将所有的权限保存在本地


                                    loadhomefactory!!.getLoagHomePage("2").LoadNewHome(context!!)//第二个代表管理端
//                                } else {
//                                }

//                            }
//                        })
//                    }
                }

                override fun setError(err: String, boolean: Boolean) {
                    setDialogding()
                    hint(err)
                }
            })
        }

    }
    var  mcount: MyCount?=null
    var  dialogLoading: DialogLoading?=null
    var loginid:String?=null
    override fun getValidateCode() {//获取验证码使用
        if (register_manage_massagtes!!.getPhone().equals("")){
            register_manage_massagtes!!.getPhoneErr().error = "手机号不能为空"
            return
        }else if(register_manage_massagtes!!.getPhone().length<11){
            register_manage_massagtes!!.getPhoneErr().error = "手机号长度不符"
            return
        }else {
            mcount = MyCount(90000, 1000)
            mcount?.start()
            dialogLoading = DialogLoading(context!!, "加载中.....", true)
            dialogLoading?.show()
            regitermodeuser.GainValidateCode(register_manage_massagtes!!.getPhone(),object :InterfaceMa<String>{
                override fun onSuccess(t: String, boolean: Boolean) {
                    if (boolean==true){
                        setDialogding()
                        loginid = t

                    }
                }

                override fun setError(err: String, boolean: Boolean) {
                    if (boolean==false){
                        setDialogding()
                        hint(err)
                        if (mcount != null) {
                            register_manage_massagtes?.getTextView()?.text="发送验证码"
                            mcount?.cancel()
                        }
                    }
                }

            })


        }
    }



    //一些提示的时候调用
    fun hint(name: String) {
        val dialog = DoubleButtonDialog(context, name)
        dialog.getokbtn().setOnClickListener(View.OnClickListener { dialog.dismiss() })
        dialog.setisVisibility()

    }
    fun setDialogding() {
        if (dialogLoading != null) {
            dialogLoading!!.dismiss()
        }
    }

    /*定义一个倒计时的内部类*/
    inner class MyCount(millisInFuture: Long, countDownInterval: Long) : CountDownTimer(millisInFuture, countDownInterval) {

        override fun onFinish() {
            register_manage_massagtes?.getTextView()?.text="发送验证码"

        }

        override fun onTick(millisUntilFinished: Long) {
            register_manage_massagtes?.getTextView()?.text="请等待90秒(" + millisUntilFinished / 1000 + ")..."

        }
    }


}